package com.example.hrsystem.ui.approval.businesstravel;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.utils.ApprovalUtils;
import com.example.hrsystem.workflow.model.BusinessTravelRequest;

import java.util.List;

/**
 * 出差申请列表适配器
 */
public class BusinessTravelRequestAdapter extends RecyclerView.Adapter<BusinessTravelRequestAdapter.BusinessTravelRequestViewHolder> {

    private List<BusinessTravelRequest> businessTravelRequests;
    private OnItemClickListener listener;

    /**
     * 构造函数
     */
    public BusinessTravelRequestAdapter(List<BusinessTravelRequest> businessTravelRequests, OnItemClickListener listener) {
        this.businessTravelRequests = businessTravelRequests;
        this.listener = listener;
    }

    /**
     * 更新数据列表
     */
    public void setBusinessTravelRequests(List<BusinessTravelRequest> businessTravelRequests) {
        this.businessTravelRequests = businessTravelRequests;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BusinessTravelRequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.business_travel_request_item, parent, false);
        return new BusinessTravelRequestViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull BusinessTravelRequestViewHolder holder, int position) {
        BusinessTravelRequest request = businessTravelRequests.get(position);
        holder.bind(request, listener);
    }

    @Override
    public int getItemCount() {
        return businessTravelRequests != null ? businessTravelRequests.size() : 0;
    }

    /**
     * 出差申请视图持有者
     */
    static class BusinessTravelRequestViewHolder extends RecyclerView.ViewHolder {
        private TextView requestIdTextView;
        private TextView travelTypeTextView;
        private TextView destinationTextView;
        private TextView dateRangeTextView;
        private TextView statusTextView;

        public BusinessTravelRequestViewHolder(@NonNull View itemView) {
            super(itemView);
            requestIdTextView = itemView.findViewById(R.id.request_id_text_view);
            travelTypeTextView = itemView.findViewById(R.id.travel_type_text_view);
            destinationTextView = itemView.findViewById(R.id.destination_text_view);
            dateRangeTextView = itemView.findViewById(R.id.date_range_text_view);
            statusTextView = itemView.findViewById(R.id.status_text_view);
        }

        public void bind(BusinessTravelRequest request, OnItemClickListener listener) {
            // 设置请求编号
            requestIdTextView.setText("申请编号: " + request.getId());

            // 设置出差类型
            travelTypeTextView.setText("出差类型: " + request.getTravelType());

            // 设置目的地
            destinationTextView.setText("目的地: " + request.getDestination());

            // 设置日期范围
            String dateRange = ApprovalUtils.formatDate(request.getStartDate()) + " 至 " + 
                               ApprovalUtils.formatDate(request.getEndDate());
            dateRangeTextView.setText("日期: " + dateRange);

            // 设置状态
            // 暂时直接使用状态值，因为ApprovalUtils.getStatusText方法不可用
            statusTextView.setText("状态: " + request.getStatus());

            // 设置点击事件
            itemView.setOnClickListener(v -> listener.onItemClick(request));
        }
    }

    /**
     * 点击事件监听器
     */
    public interface OnItemClickListener {
        void onItemClick(BusinessTravelRequest request);
    }
}